//
//  CZStatusRetweetView.swift
//  微博08
//
//  Created by apple on 16/3/3.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

// 被转发微博的view
class CZStatusRetweetView: UIView {
    
    // MARK: - 属性
    private var statusRetweetViewBottom: Constraint?
    
    /// 微博模型,用于显示被转发微博的数据
    var status: CZStatus? {
        didSet {
            // 设置内容
            let name = status?.user?.screen_name ?? "没有名称"
            let textAttr = status?.attrText ?? NSAttributedString(string: "没有微博内容")
            
            // "@\(name): \(text)"
            let nameAttr = NSMutableAttributedString(string: "@\(name):")
            nameAttr.appendAttributedString(textAttr)
            
            retweetContentLabel.attributedText = nameAttr
            
            // 判断是否有图片需要显示
            statusRetweetViewBottom?.uninstall()
            if let _ = status?.storePictureURLs {
                // 有配图需要显示设置配图视图显示内容
                retweetPictureView.hidden = false
                retweetPictureView.status = status
                
                self.snp_makeConstraints { (make) -> Void in
                    self.statusRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(CZStatusCellMargin).constraint
                }
            } else {
                retweetPictureView.hidden = true
                
                self.snp_makeConstraints { (make) -> Void in
                    self.statusRetweetViewBottom = make.bottom.equalTo(retweetContentLabel).offset(CZStatusCellMargin).constraint
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加控件
        addSubview(retweetBkgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        // 2.添加约束
        // 背景按钮
        retweetBkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        // 转发微博内容
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CZStatusCellMargin)
            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
        }
        
        // 被转发微博的配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // CZStatusRetweetView自己的高度根据子控件的内容来
        self.snp_makeConstraints { (make) -> Void in
            self.statusRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(CZStatusCellMargin).constraint
        }
    }

    // MARK: - 懒加载
    /// 背景按钮
    private lazy var retweetBkgButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    /// 转发微博内容
    private lazy var retweetContentLabel: FFLabel = {
        let label = FFLabel(textColor: UIColor.darkGrayColor(), font: CZStatusLabelFontSize)
        
        label.numberOfLines = 0
        label.labelDelegate = self
        
        // 设置折行(换行)宽度.
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * CZStatusCellMargin
        
        return label
    }()
    
    /// 被转发微博的配图
    private lazy var retweetPictureView: CZStatusPictureView = CZStatusPictureView()
}

extension CZStatusRetweetView: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        print("原创选中了: \(text)")
        
        // 判断是否是链接地址
        if text.hasPrefix("http") {
            // 弹出一个新的控制器来加载网址
            
            // 通知来让homeViewController来弹出网页控制器
            NSNotificationCenter.defaultCenter().postNotificationName(LinkTextClickNotification, object: self, userInfo: [LinkTextClickLinkKey : text])
        }
    }
}
